package quotation.R;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

@Data
@ApiModel(value = "返回类型")
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1066420789116717308L;


    @ApiModelProperty(value = "状态")
    private boolean status;
    @ApiModelProperty(value = "code")
    private Integer code;
    @ApiModelProperty(value = "错误描述")
    private String message;
    @ApiModelProperty(value = "返回对象")
    private T data;

    public R() {
    }


    public static R setResult(boolean status, Integer code, String message, Object data) {
        return new R(status, code, message, data);
    }

    public static R setResult(ResultPattern code, String message, Object data) {
        R res = new R();
        res.setStatus(code.getStatus());
        res.setCode(code);
        res.setMessage(message);
        res.setData(data);
        return res;
    }

    public static R setResult(ResultPattern code) {
        return new R(code.getStatus(), (Integer) code.getCode(), code.getMessage(), null);
    }

    public R(boolean status, Integer code, String msg, T data) {
        this.status = status;
        this.code = code;
        this.message = msg;
        this.data = data;
    }

    public static R success() {
        return success(null);
    }

    public static R success(Object data) {
        return success("成功", data);
    }

    public static R success(String msg) {
        return success(msg, null);
    }

    public static R success(String msg, Object data) {
//        return set(true, 200, msg, data);
        return set(true, 200, msg, data);
    }

    public static R fail() {
        return fail("失败");
    }

    public static R fail(String msg) {
        return fail(400, msg);
    }

    public static R fail(Integer code, String msg) {
        return set(false, code, msg, null);
    }
    public static R fail(Integer code, String msg, Object data) {
        return set(false, code, msg, data);
    }
    public static R fail(String message, Object data) {
        return setResult(ResultCode.FAIL, message, data);
    }
    public static R set(boolean status, Integer code, String msg, Object data) {
        return new R(status, code, msg, data);
    }

    /**
     * 无权限
     *
     * @return
     */
    public static R unauthorized() {
        return set(false, 403, "您暂无此权限", null);
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(ResultPattern code) {
        this.code = (Integer) code.getCode();
    }


}
